package com.csdn.leon.view;


import com.csdn.leon.base.R;

import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.os.Handler;
import android.os.Message;
import android.util.AttributeSet;
import android.util.Log;
import android.view.View;

public class CricleWheel extends View {

	private static final int DEFAULT_MAX_VALUE = 100; // 默认进度条最大值
	private static final int DEFAULT_PAINT_WIDTH = 10; // 默认画笔宽度
	private static final int DEFAULT_PAINT_COLOR = 0xffffcc00; // 默认画笔颜色
	private static final boolean DEFAULT_FILL_MODE = true; // 默认填充模式
	private static final int DEFAULT_INSIDE_VALUE = 0; // 默认缩进距离
	private final static int TIMER_ID = 0x0010;

	private int mMaxProgress;
	private int progress;

	private Paint mBigPaint;
	private Paint mSmallPaint;

	private int bigCirclRradius = 100;
	private int smallCirclRradius = 15;
	private int cirA;

	private float cirX=300, cirY=300;

	private float posX=cirX;
	private float posY=cirY -bigCirclRradius;

	private Handler mHandler = new Handler() {

		@Override
		public void handleMessage(Message msg) {

			if (msg.what == 1) {
				int a = msg.arg1;
				float cirAngle = a % 360;
				setMainProgress(cirAngle);
			}
			super.handleMessage(msg);
		}

	};

	public CricleWheel(Context context) {
		super(context);
	}

	public CricleWheel(Context context, AttributeSet attrs) {
		super(context, attrs);
		// TODO Auto-generated constructor stub

		init();

		TypedArray array = context.obtainStyledAttributes(attrs,
				R.styleable.CircleProgressBar);

		mMaxProgress = array.getInteger(R.styleable.CircleProgressBar_max,
				DEFAULT_MAX_VALUE); // 获取进度条最大值

		boolean bFill = array.getBoolean(R.styleable.CircleProgressBar_fill,
				DEFAULT_FILL_MODE); // 获取填充模式
		int paintWidth = array.getInt(
				R.styleable.CircleProgressBar_Paint_Width, DEFAULT_PAINT_WIDTH); // 获取画笔宽度

		array.recycle(); // 一定要调用，否则会有问题

	}

	private void init() {
		mBigPaint = new Paint();
		mBigPaint.setAntiAlias(true);
		mBigPaint.setStyle(Paint.Style.STROKE);
		mBigPaint.setStrokeWidth(8);
		mBigPaint.setColor(Color.parseColor("#3F51B5"));

		mSmallPaint = new Paint();
		mSmallPaint.setAntiAlias(true);
		mSmallPaint.setStyle(Paint.Style.FILL);
		mSmallPaint.setStrokeWidth(10);
		mSmallPaint.setColor(Color.parseColor("#F44336"));

	}

	@Override
	protected void onDraw(Canvas canvas) {
		super.onDraw(canvas);
		canvas.drawCircle(cirX, cirY, bigCirclRradius, mBigPaint);
		
		canvas.drawCircle(posX, posY, smallCirclRradius, mSmallPaint);

	}

	// 依圆心坐标，半径，扇形角度，计算出扇形终射线与圆弧交叉点的xy坐标
	public void CalcArcEndPointXY(float cirAngle) {
		// 将角度转换为弧度
		float arcAngle = (float) (Math.PI * cirAngle / 180.0);
		if (cirAngle < 90) {
			posX = cirX + (float) (Math.cos(arcAngle)) * bigCirclRradius;
			posY = cirY + (float) (Math.sin(arcAngle)) * bigCirclRradius;
		} else if (cirAngle == 90) {
			posX = cirX;
			posY = cirY + bigCirclRradius;
		} else if (cirAngle > 90 && cirAngle < 180) {
			arcAngle = (float) (Math.PI * (180 - cirAngle) / 180.0);
			posX = cirX - (float) (Math.cos(arcAngle)) * bigCirclRradius;
			posY = cirY + (float) (Math.sin(arcAngle)) * bigCirclRradius;
		} else if (cirAngle == 180) {
			posX = cirX - bigCirclRradius;
			posY = cirY;
		} else if (cirAngle > 180 && cirAngle < 270) {
			arcAngle = (float) (Math.PI * (cirAngle - 180) / 180.0);
			posX = cirX - (float) (Math.cos(arcAngle)) * bigCirclRradius;
			posY = cirY - (float) (Math.sin(arcAngle)) * bigCirclRradius;
		} else if (cirAngle == 270) {
			posX = cirX;
			posY = cirY - bigCirclRradius;
		} else {
			arcAngle = (float) (Math.PI * (360 - cirAngle) / 180.0);
			posX = cirX + (float) (Math.cos(arcAngle)) * bigCirclRradius;
			posY = cirY - (float) (Math.sin(arcAngle)) * bigCirclRradius;
		}
		invalidate();
	}

	public void setStartCircle() {
		new Thread(new Runnable() {
			public void run() {
				while (true) {
					cirA += 5;
					Message msg = new Message();
					msg.what = 1;
					msg.arg1 = cirA;
					mHandler.sendMessage(msg);
					try {
						Thread.sleep(20);
					} catch (InterruptedException e) {
						// TODO Auto-generated catch block
						e.printStackTrace();
					}
				}
			}
		}).start();

	}

	/*
	 * 设置主进度值
	 */
	public synchronized void setMainProgress(float cirAngle) {
		CalcArcEndPointXY(cirAngle);
		postInvalidate();
	}

	public int getBigCirclRradius() {
		return bigCirclRradius;
	}

	public void setBigCirclRradius(int bigCirclRradius) {
		this.bigCirclRradius = bigCirclRradius;
	}

	public int getSmallCirclRradius() {
		return smallCirclRradius;
	}

	public void setSmallCirclRradius(int smallCirclRradius) {
		this.smallCirclRradius = smallCirclRradius;
	}
	
	
}
